# /usr/bin/env python
# -*- coding:UTF-8 -*-


import time
import requests
import random
from Config.Utils import get_userAgent,get_crawlproxy
import urllib3
from  TOMCAT.xpath import tomcat_xpath,tomcat_version_xpath
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


def request(url,col):
    '''
    :param url:
    :param col:
    :return: 0表示正常处理，1表示非正常处理,2网页404
    '''
    user_agents = get_userAgent()
    proxies = {}
    proxy = get_crawlproxy()
    if proxy is not False:
        proxies = proxy

    headers = {
        "user-agent": random.choice(user_agents),
        # "connection": 'close',
        # "accept-encoding": 'gzip,deflate'  # 不建议使用br压缩，解压时brotli有冲突会报错
    }

    response = requests.get(url, headers = headers, verify = False,
                            timeout = 30,
                            proxies = proxies,
                            )
    response.encoding = "utf-8"
    if response.status_code == 200:
        html = response.text
        return_value=tomcat_xpath(html,col)
        # return_value = 0

        if return_value == 0:  # 正常处理，因为这个网页都是存在的没有不存在的
            return 0
        else:  # 不正常处理
            return 1
    else:  # 如果是404或者网络超时,会重试
        print('404网页')
        return 2

def reTry(reTryTime,fun,*args):
    '''
    :param reTryTime: 重试次数
    :param fun: 需要重试的函数
    :param args: 函数所需要的参数
    :return:0重试成功，1收到404重试失败，2网络连接超时的重试失败
    '''


    retry = reTryTime  # 重试次数
    while retry > 0:
        try:
            flag=fun(*args)
            if flag==0:
                print('成功')
                return 0
            elif flag==2:
                retry = retry - 1
                print('retry {}'.format(retry))
                continue

        except Exception as e:
            retry = retry - 1
            print(e)
            print('retry {}'.format(retry))
            time.sleep(3)
            if retry <= 0:
                print('网络超时重试失败')
                return 2  # 说明超时
            continue
    if retry==0:
        print('重试失败')
        return 1

if __name__ == '__main__':
    url='https://tomcat.apache.org/security-7.html'
    col=1
    reTry(10,request,url,col)
